package com.yudean.erp;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class CodeGenerator {
    public static void main(String[] args) {
        //代码生成器
        AutoGenerator generator = new AutoGenerator();
        //全局配置
        GlobalConfig config = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        config.setOutputDir(projectPath + "/src/main/java");
        config.setAuthor("沈泽群");
        config.setOpen(false);
        //是否覆盖
        config.setFileOverride(true);
        config.setSwagger2(true);
        generator.setGlobalConfig(config);

        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
        dataSourceConfig.setDriverName("oracle.jdbc.driver.OracleDriver");
        dataSourceConfig.setUsername("system");
        dataSourceConfig.setPassword("123");
        dataSourceConfig.setDbType(DbType.ORACLE);
        dataSourceConfig.setTypeConvert(new OracleTypeConvert());
        generator.setDataSource(dataSourceConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.yudean.erp");
        generator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("IDS_PM_PRO_TECH");//可变参数，设置要映射的表名 //ids_pm_pro_tech
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setRestControllerStyle(true);
        generator.setStrategy(strategyConfig);

        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                //to do nothing
            }
        };

        generator.setCfg(injectionConfig);

        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }
}
